package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import pojo.Area;
import util.DBUtils;
import dao.AreaDao;

public class AreaDaoImpl implements AreaDao{

	@Override
	public List<Area> getAreasByLikeName(String likeName) {
		String sql = "select * from areas where area like ?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Area> areas = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, likeName+"%");
			rs = pst.executeQuery();
			while(rs.next()) {
				Area area = new Area();
				area.setId(rs.getInt("id"));
				area.setAreaId(rs.getString("areaid"));
				area.setArea(rs.getString("area"));
				area.setCityId(rs.getString("cityid"));
				areas.add(area);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return areas;	
	}

}
